package btreegui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Yarolye
 */
public class GuiContainer {

    private List<GuiContainer> childs = new ArrayList<GuiContainer>();
    private DrawTreeElementBucket bucket;
    private int actualHight;
    private int reihenfolge;
    private GuiContainer fatherContainer = null;
    private Dimension guiCLocation = new Dimension(0, 0);
    private Dimension guiCSize = new Dimension(0, 0);
    public static int[] offsets;
    public int widthOfChildren;
    private int verZweigungsgrad;

    public GuiContainer(BTreeGUI frame) {
        bucket = new DrawTreeElementBucket(frame);

        try {
            verZweigungsgrad = Integer.parseInt(frame.getJsVerzweigungsgrad());
            verZweigungsgrad = verZweigungsgrad * 2;
        } catch (NumberFormatException ex) {
            verZweigungsgrad = 2;
        }
        bucket.setbSize(verZweigungsgrad);
    }

    public void calculatePosition() {
        getReihenfolge();
    }

    public void addChild(GuiContainer childContainer) {
        childContainer.setReihenfolge(getReihenfolgeForNewChild());
        childContainer.setFatherContainer(this);
        childs.add(childContainer);
        childContainer.bucket.setbSize(verZweigungsgrad);
    }

    public void addValue(String treeValue, Color back, Color fore) {
        this.bucket.addTreeElement(treeValue, back, fore);
    }

    public DrawTreeElementBucket getBucket() {
        return bucket;
    }

    public int getReihenfolge() {
        return reihenfolge;
    }

    public void setReihenfolge(int position) {
        this.reihenfolge = position;
    }

    private int getReihenfolgeForNewChild() {
        return childs.size() + 1;
    }

    public int getActualHight() {
        return actualHight;
    }

    public void setActualHight(int actualHight) {
        this.actualHight = actualHight;
    }

    public int getHightOfTree() {

        GuiContainer child = null;
        if (!childs.isEmpty()) {
            child = childs.get(0);
        }
        int hightOfChilds = 0;
        if (child != null) {
            hightOfChilds = child.getHightOfTree();
        } else {
            hightOfChilds = actualHight;
        }
        return hightOfChilds;
    }

    public void paintComponent(Graphics2D g2d) {
        bucket.drawBucket(g2d);
        for (GuiContainer a : childs) {
            a.getBucket().drawBucket(g2d);
            a.paintComponent(g2d);
            g2d.setColor(Color.BLACK);
            if (!childs.isEmpty()) {
                g2d.drawLine(bucket.getbLocation().width + (a.getReihenfolge() - 1) * DrawTreeElement.getWidth(), bucket.getbLocation().height + DrawTreeElement.getHeight(), a.getBucket().getbLocation().width + (a.getBucket().getbSize().width - DrawTreeElement.getWidth()) / 2, a.getBucket().getbLocation().height);
            }
        }
    }

    public GuiContainer getFatherContainer() {
        return fatherContainer;
    }

    public void setFatherContainer(GuiContainer fatherContainer) {
        this.fatherContainer = fatherContainer;
    }

    public void setPosition() {
        offsets = null;
        offsets = new int[getHightOfTree() + 1];
        setPositionOfCont();
    }

    public void setPositionOfCont() {
        for (GuiContainer guiContainer : childs) {      //im tree ganz nach unten gehen
            guiContainer.setPositionOfCont();
        }
        int tmpWidth = widthOfChildren;
        if (tmpWidth == 0) {
            tmpWidth = bucket.getbSize().width;
        }
        int xPosition = offsets[actualHight] + bucket.getbSize().width;
        int yPosition = actualHight * 2 * DrawTreeElement.getHeight();
        GuiContainer.offsets[actualHight] += tmpWidth;
        guiCLocation = new Dimension(xPosition, yPosition);
        if (getFatherContainer() != null) {
            getFatherContainer().widthOfChildren = getFatherContainer().widthOfChildren + tmpWidth;
        }
        int size = widthOfChildren;
        if (size == 0) {
            bucket.setbLocation(guiCLocation);
        } else {
            bucket.setbLocation(new Dimension(xPosition + size / 2 - bucket.getbSize().width / 2, guiCLocation.height));
        }
    }

    public void setGuiCLocation(Dimension guiCLocation) {
        this.guiCLocation = guiCLocation;
    }

    public void setGuiCbSize(Dimension guiCbSize) {
        this.guiCSize = guiCbSize;
    }

    public Dimension getGuiCLocation() {
        return guiCLocation;
    }

    public Dimension getGuiCbSize() {
        return guiCSize;
    }

    public List<GuiContainer> getChilds() {
        return childs;
    }
}
